/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.service.impl;

import java.util.List;
import java.util.Map;

import com.cq.base.domain.Page;
import com.cq.bd.dao.SStaffDao;
import com.cq.bd.dao.SWorkerDao;
import com.cq.bd.dao.SWorkerFunctionDao;
import com.cq.bd.dao.SWorkerRoleDao;
import com.cq.bd.domain.SStaff;
import com.cq.bd.domain.SWorker;
import com.cq.bd.domain.SWorkerFunction;
import com.cq.bd.domain.SWorkerRole;
import com.cq.bd.dto.WorkerDto;
import com.cq.bd.service.WorkerService;

/**
 * 创建用户和工号
 * 
 * @author huangmy
 * @date   2009-10-5
 */
@SuppressWarnings("unchecked")
public class WorkerServiceImpl implements WorkerService {
    private SStaffDao sStaffDao;
    private SWorkerDao sWorkerDao;
    private SWorkerRoleDao sWorkerRoleDao;    
    private SWorkerFunctionDao sWorkerFunctionDao;
    
    @SuppressWarnings("unchecked")
    public List<SWorkerFunction> doGetWorkerFuncs(Long loginId) {
        return this.sWorkerFunctionDao.find("from SWorkerFunction where loginId=? and state=1", loginId);
    }
    
    public List<SWorker> doGetWorkersByLoginCode(String loginCode) {
        return this.sWorkerDao.find("from SWorker where loginCode=?", loginCode);
    }
    
    @SuppressWarnings("unchecked")
    public List<SWorker> doGetWorkersByStaffId(Long staffId) {
        return this.sWorkerDao.find("from SWorker where staffId=?", staffId);
    }
    
    public Page pageWorkers(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {
        return this.sWorkerDao.pagedQuery(filter, like, sort, pageNo, pageSize);
    }
    
    public SWorker doGetWorker(Long loginId) {
        SWorker w = this.sWorkerDao.get(loginId);
        return w;
    }
    
    public void doUpdateWorker(SWorker worker) {
        this.sWorkerDao.update(worker);
    }
    
    @SuppressWarnings("unchecked")
    public List<SWorkerRole> doGetWorkerRoles(Long loginId) {
        return this.sWorkerRoleDao.find("from SWorkerRole where loginId=?", loginId);
    }
    
    public void doCreateWorkerRole(SWorkerRole workerRole) {
        this.sWorkerRoleDao.create(workerRole);
    }
    
    public void doRemoveWorkerRole(SWorkerRole workerRole) {
        this.sWorkerRoleDao.remove(workerRole);
    }
    
    public void doCreateWorkerFunc(SWorkerFunction workerFunc) {
        this.sWorkerFunctionDao.create(workerFunc);
    }
    
    public void doRemoveWorkerFunc(SWorkerFunction workerFunc) {
        this.sWorkerFunctionDao.remove(workerFunc);
    }
    
    public void doUpdateWorkerFunc(SWorkerFunction workerFunc) {
        this.sWorkerFunctionDao.update(workerFunc);
    }
    
    public List<SWorker> doGetWorkers() {
        return this.sWorkerDao.list();
    }
    
    protected WorkerDto createWorkerDto(SStaff s, SWorker w) {
        WorkerDto d = new WorkerDto();
        d.setAge(s.getAge());
        d.setBirthday(s.getBirthday());
        d.setCardNo(s.getCardNo());
        d.setCardType(s.getCardType());
        d.setDepartId(s.getDepartId());
        d.setEmail(s.getEmail());
        d.setLoginCode(w.getLoginCode());
        d.setLoginPwd(w.getLoginPwd());
        d.setLoginType(w.getLoginType());
        d.setOtherRelaPhone(s.getOtherRelaPhone());
        d.setPlaceId(s.getPlaceId());
        d.setRegionId(s.getRegionId());
        d.setRelaAddr(s.getRelaAddr());
        d.setRelaMobile(s.getRelaMobile());
        d.setRelaPhone(s.getRelaPhone());
        d.setSendPhone(w.getSendPhone());
        d.setSex(s.getSex());
        d.setStaffName(s.getStaffName());
        d.setStaffType(s.getStaffType());
        d.setState(s.getState());
        d.setLoginId(w.getLoginId());
        
        return d;
    }

    public void doCreateWorker(SWorker worker) {        
        this.sWorkerDao.create(worker);
    }

    public void doChangeWorkerState(Long loginId, int state) {
        SWorker w = this.sWorkerDao.get(loginId);
        if (w != null) {
            w.setState(state);
            this.sWorkerDao.update(w);
            
            SStaff s = this.sStaffDao.get(w.getStaffId());
            if (s != null) {
                s.setState(state);
                this.sStaffDao.update(s);
            }
        }
    }

    public void setSStaffDao(SStaffDao staffDao) {
        sStaffDao = staffDao;
    }

    public void setSWorkerDao(SWorkerDao workerDao) {
        sWorkerDao = workerDao;
    }

    public void setSWorkerRoleDao(SWorkerRoleDao workerRoleDao) {
        sWorkerRoleDao = workerRoleDao;
    }

    public void setSWorkerFunctionDao(SWorkerFunctionDao workerFunctionDao) {
        sWorkerFunctionDao = workerFunctionDao;
    }
}
